# -*- coding: utf-8 -*-
"""
Module Description:
Date: 2020/10/01
Author: zhongzhaopai
"""
from pyshare.const.enums.cross_island_enum import DebrisType
from pyshare.game_logs.cross_island_log import CrossIslandVitLog, CrossIslandPersonScoreLog, CrossIslandAlliesScoreLog, \
    CrossIslandGemLog, CrossIslandFirstDebrisLog, CrossIslandSecondDebrisLog
from pyshare.utils.time_utils import getCT, isPassDay
from pyshare.const.enums.payment import UnlockType


class CrossIslandMiXin(object):

    activity_id = NotImplemented
    role_guid = NotImplemented
    grid_num = NotImplemented
    scene_id = NotImplemented
    vit_buy_count_stat = NotImplemented
    last_refresh_time = NotImplemented
    debris_data = NotImplemented
    scene_affair_dict = NotImplemented
    compass_affair_dict = NotImplemented
    guarantee_affair_stat = NotImplemented
    life_num = NotImplemented
    fight_assist_vit_buy_count = NotImplemented
    boss_attack_count = NotImplemented
    destiny_guard_dict = NotImplemented
    buff_dict = NotImplemented
    use_buff_dict = NotImplemented
    vit = NotImplemented
    score = NotImplemented
    allies_score = NotImplemented
    gem = NotImplemented

    def role_vit_log(self, val):
        """
        体力日志
        :param val:
        :return:
        """
        add_vit = val - self.vit.remaining
        if add_vit == 0:
            return
        try:
            CrossIslandVitLog.log_game(self.role_guid, val, add_vit)
        except:
            pass

    @property
    def is_unlock_special_award(self):
        """
        特殊奖励是否解锁
        :return:
        """
        from pyshare.dal.repository_impl import RechargeUnlockRepository

        unlock_type = 0
        primary_record_count = RechargeUnlockRepository.get_count_by_rid_unlockid_actid(self.role_guid,
                                                                                        UnlockType.CrossIslandPrimaryAward,
                                                                                        self.activity_id)
        if primary_record_count:
            unlock_type = UnlockType.CrossIslandPrimaryAward
        advance_record_count = RechargeUnlockRepository.get_count_by_rid_unlockid_actid(self.role_guid,
                                                                                        UnlockType.CrossIslandAdvancedAward,
                                                                                        self.activity_id)
        if advance_record_count:
            unlock_type = UnlockType.CrossIslandAdvancedAward
        return unlock_type

    def person_score_value_log(self, val):
        """
        个人积分日志
        :param val: 积分数值
        :return:
        """
        add_score_value = val - self.score
        if add_score_value == 0:
            return
        try:
            CrossIslandPersonScoreLog.log_game(self.role_guid, val, add_score_value)
        except:
            pass

    def allies_score_value_log(self, val):
        """
        盟国积分日志
        :param val: 积分数值
        :return:
        """
        add_score_value = val - self.allies_score
        if add_score_value == 0:
            return
        try:
            CrossIslandAlliesScoreLog.log_game(self.role_guid, val, add_score_value)
        except:
            pass

    def gem_value_log(self, val):
        """
        宝石日志
        :param val: 宝石数值
        :return:
        """
        add_gem_value = val - self.gem
        if add_gem_value == 0:
            return
        try:
            CrossIslandGemLog.log_game(self.role_guid, val, add_gem_value)
        except:
            pass

    def debris_log(self, val):
        """
        碎片日志
        :param val: 碎片详情
        :return:
        """
        try:
            debris_list = [str(DebrisType.First), str(DebrisType.Second)]
            for debris_id in debris_list:
                new_val = val.get(debris_id, 0)
                old_val = self.debris_data.get(debris_id, 0)
                add_debris_value = new_val - old_val
                if add_debris_value == 0:
                    continue
                if debris_id == str(DebrisType.First):
                    try:
                        CrossIslandFirstDebrisLog.log_game(self.role_guid, new_val, add_debris_value)
                    except:
                        pass
                else:
                    try:
                        CrossIslandSecondDebrisLog.log_game(self.role_guid, new_val, add_debris_value)
                    except:
                        pass
        except:
            pass

    @staticmethod
    def format_pvp_affair_data(select_pvp_dict):
        """
        返回事件相关数据
        :return:
        """
        if not select_pvp_dict:
            return select_pvp_dict
        from pyshare.bll.utils.share.share_role_information import ShareRoleInformation
        from pyshare.bll.utils.share.allies_information import AlliesCountryCache
        rid_list = []
        server_dict = {}
        allies_id_country_dict = {}
        for rid, info_dict in select_pvp_dict.iteritems():
            rid_list.append(rid)
            server_id = info_dict.get("ServerId")
            if server_id in server_dict:
                server_dict[server_id].append(rid)
            else:
                server_dict.update({server_id: [rid]})
            allies_id = info_dict.get("AlliesId")
            country_id = info_dict.get("CountryId")
            if allies_id in allies_id_country_dict:
                if country_id in allies_id_country_dict[allies_id]:
                    continue
                allies_id_country_dict[allies_id].append(country_id)
            else:
                allies_id_country_dict[allies_id] = [country_id]

        role_info_dict = ShareRoleInformation.get_many_role_information_from_cache_db(server_dict)
        allies_cache_data = AlliesCountryCache.get_many_country_info_from_cache_db(allies_id_country_dict)
        for rid, info_dict in select_pvp_dict.iteritems():
            a_id_c_id = AlliesCountryCache.get_key(info_dict["AlliesId"], info_dict["CountryId"])
            info_dict.update({
                "NickName": role_info_dict[rid].nick_name,
                "HeadId": role_info_dict[rid].head_id,
                "SmallHeadId": role_info_dict[rid].small_head_id,
                "AlliesNo": allies_cache_data[a_id_c_id].AlliesNo,
                "CountryName": allies_cache_data[a_id_c_id].CountryName,
                "NameChangeTimes": allies_cache_data[a_id_c_id].NameChangeTimes,
                "LastSeasonDan": role_info_dict[rid].last_season_dan,
                "ClothesColor": role_info_dict[rid].clothes_color
            })
        return select_pvp_dict
